﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;

namespace Yuanlin.Business
{
    public class AquacultureAppCategoryEditControl : YuanlinUserControl,
        IReplyConverter<AquacultureAppCategoryReply, GetOperation>
    {
        protected ValuedTextBox titleValuedTextBox;
        protected IButton SaveButton;
        protected IButton BackButton;
        private AquacultureAppCategory aquacultureAppCategory;
        private int categoryType = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryType);

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            if (!Page.IsPostBack)
            {
                requestBinders.Add(RequestHelper.BuildGetAquacultureAppCategoryRequest(this));
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
        }
        private void Bind()
        {
            if (aquacultureAppCategory != null)
            {
                this.titleValuedTextBox.Value = aquacultureAppCategory.Title.Value;
            }   
        }
        private void PageRedirect()
        {
            Page.Response.Redirect(SiteUrls.Instance().GetAquacultureAppCategoryListUrl(categoryType));
        }
        public void BackButton_Click(object sender, EventArgs e)
        {
            PageRedirect();
        }
        public void SaveButton_Click(object sender, EventArgs e)
        {
            if (aquacultureAppCategory != null)
            {
                aquacultureAppCategory.Title.Value = CensorWordManager.CensorWord(this.titleValuedTextBox.Value);
                aquacultureAppCategory.CategoryType.Value = categoryType;

                if (BusinessManager.UpdateAquacultureAppCategory(aquacultureAppCategory) == -2)
                {
                    Globals.ShowMessage(this.Page, "修改失败", "此信息已经存在！");
                    return;
                }
            }

            PageRedirect();
        }

        #region IReplyConverter<AquacultureAppCategoryReply,GetOperation> Members

        public void ConvertReply(AquacultureAppCategoryReply reply, GetOperation operation)
        {
            if (reply != null)
            {
                aquacultureAppCategory = reply.Entity as AquacultureAppCategory;

                if (!Page.IsPostBack)
                {
                    Bind();
                }
            }
        }

        #endregion
    }
}